package com.pinc.exam;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class Test_Overview extends WidgetActivity {
	private Summary_Adapter mAdapter;
	private ListView lv;
	private Question_Packet mQP;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_layout);
		initWidget();

		new ShowTask().execute(this);
	}

	private class ShowTask extends AsyncTask<Context, Void, Integer> {

		protected Integer doInBackground(Context... ctx) {
			mQP = (Question_Packet) getIntent().getExtras().getSerializable("packet");

			mAdapter = new Summary_Adapter(Test_Overview.this);

			for ( int i=0; i<mQP.mQstn_total; i++)
			{
				Summary_Class sc = new Summary_Class(mQP.mArr_Qid[i], 
						mQP.mQuestion[i],
						mQP.mArr_ansPaper[i],
						null);
				mAdapter.add(sc);
			}
			return 0;
		}

		@Override
		protected void onPostExecute(Integer err) {
			lv.setAdapter(mAdapter);
			lv.setOnItemClickListener(new sumItemListener()); 
		}
	}

	@Override
	protected void initWidget() {
		super.initWidget();

		lv = (ListView) this.findViewById(R.id.listView1);

		tv_title.setText(getResources().getString(R.string.str_overview));

		b_prev.setVisibility(View.VISIBLE);
		b_prev.setText(getResources().getString(R.string.str_back));
		b_prev.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		b_next.setVisibility(View.VISIBLE);
		b_next.setText(getResources().getString(R.string.str_submit));
		b_next.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if ( isAllMade() )
					submit();
				else
					showDialog(ID_WARN);
			}
		});
	}

	private void submit()
	{
		deleteFile(GParam.QUIZ_FILENAME);
		Intent intent = new Intent(Test_Overview.this, Test_Result.class);
		intent.putExtra("packet", mQP);
		startActivity(intent);
		setResult(Activity.RESULT_FIRST_USER);
		finish();
	}

	private boolean isAllMade()
	{
		for(int i=0;i<mQP.mQstn_total;i++)
		{
			if ( mQP.mArr_ansPaper[i] == 0)
				return false;
		}
		return true;
	}

	private class sumItemListener implements OnItemClickListener
	{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,	long id) {
			Intent intent = new Intent();
			intent.putExtra("number", position);
			setResult(Activity.RESULT_OK, intent);
			finish();
		}
	}

	private static final int ID_WARN = 0;
	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		switch(id)
		{
		case ID_WARN:
		{
			builder.setMessage(getResources().getString(R.string.str_not_answered))
			.setPositiveButton(getResources().getString(R.string.str_ok), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					submit();
				}
			})
			.setNegativeButton(getResources().getString(R.string.str_cancel), null);
			break;
		}
		default:
			break;
		}
		return builder.create();
	}

}
